﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models.WorkFlow.Dtos.FlowMonitor;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 流程监控
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-06-11
    /// </summary>
    [Route("api/WorkFlow/[controller]")]
    public class FlowMonitorController : BaseApiController
    {
        private FlowTaskBll flowTaskBll = new FlowTaskBll();
        private FlowEngineBll flowEngineBll = new FlowEngineBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] FlowMonitorListQuery input)
        {
            var page = input.MapTo<PageInput>();
            page.queryJson = new { input.endTime, input.startTime, input.creatorUserId, input.flowId, input.flowCategory }.ToJson();
            var data = await flowTaskBll.GetMonitorList(page);
            var outdata = data.list.MapToList<FlowMonitorListOutput>();
            var flowEngineList = await flowEngineBll.GetList();
            foreach (var item in outdata)
            {
                var entity = flowEngineList.Find(x => x.Id == item.flowId);
                if (entity != null)
                {
                    item.formData = entity.FlowTemplateJson.IsNotEmptyOrNull() ? entity.FlowTemplateJson : "";
                    item.formType = entity.FormType.IsNotEmptyOrNull() ? entity.FormType : 2;
                }

            }
            var output = new PageResult() { list = outdata, pagination = data.pagination };
            return Success(output);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResponseResult> Delete([FromBody] FlowMonitorDeleteInput input)
        {
            var ids = new List<string>(input.ids.Split(","));
            var entityList = ids.Select(x => flowTaskBll.GetInfo(x).Result).ToList();
            await flowTaskBll.Delete(entityList);
            return Success("删除成功");
        }
        #endregion
    }
}
